﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEditor.Callbacks;

using System.IO;
using System.Collections;


public class ReporterEditor : Editor
{
    [MenuItem("Reporter/Create")]
    public static void CreateReporter()
    {
        const int ReporterExecOrder = -12000;
        GameObject reporterObj = new GameObject();
        reporterObj.name = "Reporter";

        Reporter reporter = reporterObj.AddComponent<Reporter>();
        reporterObj.AddComponent<ReporterMessageReceiver>();
        //reporterObj.AddComponent<TestReporter>();

        // Register root object for undo.
        Undo.RegisterCreatedObjectUndo(reporterObj, "Create Reporter Object");

        MonoScript reporterScript = MonoScript.FromMonoBehaviour(reporter);
        string reporterPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(reporterScript));

        if (MonoImporter.GetExecutionOrder(reporterScript) != ReporterExecOrder)
        {
            MonoImporter.SetExecutionOrder(reporterScript, ReporterExecOrder);
            //Debug.Log("Fixing exec order for " + reporterScript.name);
        }

        reporter.images = new Images();
        reporter.images.logImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_log_icon.png"), typeof(Texture2D));
        reporter.images.warningImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_warning_icon.png"), typeof(Texture2D));
        reporter.images.errorImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_error_icon.png"), typeof(Texture2D));
        reporter.images.barImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_bar.png"), typeof(Texture2D));
        reporter.images.button_activeImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_button_active.png"), typeof(Texture2D));
        reporter.images.even_logImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_even_log.png"), typeof(Texture2D));
        reporter.images.odd_logImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_odd_log.png"), typeof(Texture2D));
        reporter.images.selectedImage = (Texture2D)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/Reporter_selected.png"), typeof(Texture2D));

        reporter.images.reporterScrollerSkin = (GUISkin)AssetDatabase.LoadAssetAtPath(Path.Combine(reporterPath, "Images/reporterScrollerSkin.guiskin"), typeof(GUISkin));
    }
}

public class ReporterModificationProcessor : UnityEditor.AssetModificationProcessor
{
    [InitializeOnLoad]
    public class BuildInfo
    {
        static BuildInfo()
        {
            EditorApplication.update += Update;
        }

        static bool isCompiling = true;
        static void Update()
        {
            if (!EditorApplication.isCompiling && isCompiling)
            {
                //Debug.Log("Finish Compile");
                if (!Directory.Exists(Application.dataPath + "/StreamingAssets"))
                {
                    Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");
                }
                string info_path = Application.dataPath + "/StreamingAssets/build_info.txt";
                StreamWriter build_info = new StreamWriter(info_path);
                build_info.Write("Build from " + SystemInfo.deviceName + " at " + System.DateTime.Now.ToString());
                build_info.Close();
            }

            isCompiling = EditorApplication.isCompiling;
        }
    }
}
